package com.example.combwyaq.visualinterface.news;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.PhoneUtils;
import com.example.combwyaq.R;
import com.example.combwyaq.db.DaoManager;
import com.example.combwyaq.entity.UserEntity;
import com.example.combwyaq.mvp.contract.LREContract;

import com.example.combwyaq.mvp.di.DaggerLREComponent;
import com.example.combwyaq.mvp.di.LREModules;
import com.example.combwyaq.mvp.model.HttpCode;
import com.example.combwyaq.mvp.model.LREModel;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.model.entity.ChannelEntity;
import com.example.combwyaq.mvp.model.entity.TextBannerEntity;
import com.example.combwyaq.visualinterface.news.adapter.NewsVpAdapter;
import com.example.combwyaq.visualinterface.news.adapter.TextBannerAdapter;
import com.example.combwyaq.mvp.presenter.LREPresenter;
import com.example.combwyaq.mvp.viwe.BaseFragment;
import com.example.combwyaq.utils.ToastUtils;
import com.example.combwyaq.visualinterface.activity.NewsdetailsMainActivity;
import com.flyco.tablayout.SlidingTabLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsFragment extends BaseFragment<LREPresenter> implements LREContract.LREView ,
        OnBannerListener<TextBannerEntity.Values> {
    private static final String TAG = "NewsFragment";
    private SlidingTabLayout tabLayout;
    private ViewPager viewPager;
    private Banner<TextBannerEntity.Values,TextBannerAdapter> news_text_banner;
    private TextBannerAdapter textBannerAdapter;
    private NewsVpAdapter newsVpAdapter;
    private List<Fragment> fragments;

    @Override
    public void refreshAll(BaseEntity entity) {
        Log.i(TAG, "refreshAll: ");
        if (entity instanceof TextBannerEntity){
            //文字banner返回
            TextBannerEntity textBannerEntity = (TextBannerEntity) entity;
//            LogUtils.e("textBannerEntity:"+textBannerEntity.values);
            if (textBannerEntity.getValues() == null) {
                ToastUtils.showMsg("文字广告数据丢失");
                return;
            }
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            news_text_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer())
                    .setOnBannerListener(this);
        }else if (entity instanceof ChannelEntity){
            //频道列表数据
            //按照频道数量以及频道id初始化fragments
            ChannelEntity channelEntity = (ChannelEntity) entity;
            if (channelEntity.getValues()==null){
                ToastUtils.showMsg("无频道数据");
                return;
            }
            for (int i = 0;i < channelEntity.getValues().size();i ++){
                fragments.add(new ContentNewFragment(channelEntity.getValues().get(i).channel_id));
            }
            newsVpAdapter = new NewsVpAdapter(getFragmentManager(), channelEntity.getValues(), fragments);
            viewPager.setAdapter(newsVpAdapter);
            tabLayout.setViewPager(viewPager);

        }

    }

    @Override
    public void refreshRecyclerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }



    @Override
    public int bindLayout() {
        return R.layout.fragment_news;
    }

    @Override
    public void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.INTERNET
            }, 101);
        }

        Log.i(TAG, "initView: ");
        tabLayout=f(R.id.news_tab);
        viewPager=f(R.id.news_vp);
        news_text_banner=f(R.id.news_text_banner);

    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build()
                .injectNewsFragment(this);
    }

    @Override
    public void initData() {
        Log.i(TAG, "initData: ");
        fragments=new ArrayList<>();
        //获取顶部文字banner数据源
            Map<String, Object> bannerMap = new HashMap<>();
            bannerMap.put("code", HttpCode.TEXTBANNERCODE);
            Map<String, Object> channelMap = new HashMap<>();
            channelMap.put("code",HttpCode.USERCHANNELCODE);
            channelMap.put("imie", System.currentTimeMillis()+"");
            channelMap.put("userId",1);
            p.requestAll(bannerMap,channelMap);
            Log.i("www", "initData2: "+channelMap);

    }
    @Override
    public void OnBannerClick(TextBannerEntity.Values data, int position) {
        ToastUtils.showMsg("点击广告"+data.news_title);
        Bundle bundle = new Bundle();
        bundle.putString("title",data.news_title);
        startPage(bundle,NewsdetailsMainActivity.class);
    }
    @Override
    public void onStart() {
        super.onStart();
        news_text_banner.start();
    }
    @Override
    public void onStop() {
        super.onStop();
        news_text_banner.stop();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        news_text_banner.destroy();
    }

}
